#include <iostream>
using namespace std;

// 二叉树节点的定义
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

// 检查是否存在一条路径的和等于给定值
bool hasPathSum(TreeNode* root, int sum) {
    if (root == nullptr) {
        return false;
    }
    if (root->left == nullptr && root->right == nullptr && sum == root->val) {
        return true;
    }
    return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

int main() {
    // 创建一棵二叉树
    TreeNode* root = new TreeNode(5);
    root->left = new TreeNode(4);
    root->right = new TreeNode(8);
    root->left->left = new TreeNode(11);
    root->left->left->left = new TreeNode(7);
    root->left->left->right = new TreeNode(2);
    root->right->left = new TreeNode(13);
    root->right->right = new TreeNode(4);
    root->right->right->right = new TreeNode(1);

    // 检查是否存在一条路径的和等于给定值
    int sum = 22;
    bool hasSum = hasPathSum(root, sum);
    if (hasSum) {
        cout << "There exists a path with sum " << sum << " in the binary tree." << endl;
    } else {
        cout << "There does not exist a path with sum " << sum << " in the binary tree." << endl;
    }

    return 0;
}
